import lightgbm as lgb
from tiancheng.base.base_helper import *
from sklearn.model_selection import StratifiedKFold


sub = get_sub()
label = get_tag_train_new()[tag_hd.Tag].values
# op_train_weo_ftr = pd.read_csv(features_base_path+"op_train_weo_ftr.csv")
# op_train_weo_ftr.pop(tag_hd.Tag)
# trsct_train_weo_ftr = pd.read_csv(features_base_path+"trsct_train_weo_ftr.csv")
# trsct_train_weo_ftr.pop(tag_hd.Tag)
# train = op_train_weo_ftr.merge(trsct_train_weo_ftr, on='UID', how='left')
# train = train.values
# op_test_weo_ftr = pd.read_csv(features_base_path+"op_test_weo_ftr.csv")
# trsct_test_weo_ftr = pd.read_csv(features_base_path+"trsct_test_weo_ftr.csv")
# test = op_test_weo_ftr.merge(trsct_test_weo_ftr, on='UID', how='left')
train, label, cols = get_X_y_weo()
test = get_test_weo()
test = test.values
print(test.shape)
print(train.shape)

skf = StratifiedKFold(n_splits=5, random_state=2018, shuffle=True)
best_score = []

oof_preds = np.zeros(train.shape[0])
sub_preds = np.zeros(test.shape[0])
res, weights = pd.DataFrame(), []
for index, (train_index, test_index) in enumerate(skf.split(train, label)):
    lgb_model = lgb.LGBMClassifier(boosting_type='gbdt', num_leaves=5000, reg_alpha=3, reg_lambda=5, max_depth=-1,
                                   n_estimators=5000, objective='binary', subsample=0.95, colsample_bytree=0.77,
                                   subsample_freq=1, learning_rate=0.03,
                                   class_weight='balanced',
                                   random_state=1000+index, n_jobs=4, min_child_weight=8, min_child_samples=8,
                                   min_split_gain=0.2)
    lgb_model.fit(train[train_index], label[train_index], verbose=10,
                  eval_metric="binary_logloss",
                  eval_set=[(train[train_index], label[train_index]),
                            (train[test_index], label[test_index])], early_stopping_rounds=50)
    best_score.append(lgb_model.best_score_['valid_1']['binary_logloss'])
    print(best_score)
    oof_preds[test_index] = lgb_model.predict_proba(train[test_index], num_iteration=lgb_model.best_iteration_)[:,1]
    m = tpr_weight_funtion(y_predict=oof_preds[test_index], y_true=label[test_index])
    print(m)
    if m < 0.79:
        continue
    test_pred = lgb_model.predict_proba(test, num_iteration=lgb_model.best_iteration_)[:, 1]
    weights.append(m)
    res[index] = test_pred
    # sub_preds += test_pred / 5
    # print('test mean:', test_pred.mean())
    # predict_result['predicted_score'] = predict_result['predicted_score'] + test_pred

print(weights)
res.columns = [i for i in range(res.shape[1])]
blending_model(res, weights, "baseline08_")
print(123)
# index = weights.index(max(weights))
# sub['Tag'] = res[index]
# print(max(weights))
# sub.to_csv(sub_base_path + 'baseline06_%s.csv' % str(weights[index]), index=False)

